package com.wyp168.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyp168.serviceedu.client.VodClient;
import com.wyp168.serviceedu.entity.EduVideo;
import com.wyp168.serviceedu.mapper.EduVideoMapper;
import com.wyp168.serviceedu.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author wyp168
 * @since 2022-05-04
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    //根据课程id删除小节
    //TODO 删除小节，删除对应视频文件
    @Override
    public void removeVideoByCourseId(String courseId) {
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id",courseId);
        videoWrapper.select("video_source_id");
        List<EduVideo> videoIdList = baseMapper.selectList(videoWrapper);
        StringBuilder sb = new StringBuilder();
        for (int index = 0;index < videoIdList.size(); index++) {
            if (videoIdList.get(index).getVideoSourceId() != null) {
                sb.append(videoIdList.get(index).getVideoSourceId()).append(",");
            }
        }
        vodClient.deleteBatchVideo(sb.toString());

        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
